package com.itheima.health.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.health.common.MessageConst;
import com.itheima.health.entity.Result;
import com.itheima.health.pojo.Menu;
import com.itheima.health.service.MenuService;
import com.itheima.health.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

import com.itheima.health.entity.PageResult;
import com.itheima.health.entity.QueryPageBean;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author: Nic1
 * @Date: 2020/8/26 9:20
 * @Theme:
 */
@RestController
@RequestMapping("/menu")
@Slf4j
public class MenuController {

    @Reference
    MenuService menuService;

    @Reference
    UserService userService;

    /**
     * 根据用户名获取当前用户可以查看的菜单栏
     * @return
     */
    @RequestMapping("/getMenu")
    public Result getMenuByUserName(String username){
        log.info("_myData_查询的用户名{}",username);
        List<Long> roleIds = userService.getRoleIdByUsername(username);
        List<Menu> menuList = menuService.queryMenuByRoleId(roleIds);
        return new Result(true, MessageConst.GET_MENU_SUCCESS, menuList);

    @GetMapping("/findPage")
    public Result findPage(QueryPageBean queryPageBean) {
        log.info("[检查项-分页查询]data:{}", queryPageBean);
        //rpc查询数据
        PageResult pageResult = menuService.findPage(queryPageBean);
        //封装返回结果
        return new Result(true, MessageConst.ACTION_SUCCESS,pageResult);
    }

    @GetMapping("/findFuMenuNameAndId")
    public Result findFuMenuNameAndId(){
        List<Menu> list=menuService.findFuMenuNameAndId();
        return new Result(true,"查询父级菜单项成功",list);
    }

    @RequestMapping("/addMenu")
    public Result addMenu(@RequestBody Menu menu){
        log.info("[添加菜单项]data:{}",menu);
        menuService.addMenu(menu);
        return new Result(true,"添加菜单成功");
    }

    @RequestMapping("/editMenu")
    public Result editMenu(@RequestBody Menu menu){
        log.info("[添加菜单项]data:{}",menu);
        menuService.editMenu(menu);
        return new Result(true,"编辑菜单成功");
    }

    @RequestMapping("/delete")
    public Result delete(Integer id){
        log.info("[检查项-根据id删除]id：{}",id);
        //RPC调用处理业务
        menuService.delete(id);
        return new Result(true,"删除菜单成功");
    }
}
